import { showToast } from "vant";
import useClipboard from "vue-clipboard3";
const { toClipboard } = useClipboard();
/**
 * @desc 去除空格
 * @param {string} str - 字符串
 * @param {string} pos - 去除空格的位置
 * pos="both": 去除两边空格
 * pos="left": 去除左边空格
 * pos="right": 去除右边空格
 * pos="all": 去除所有空格 */
export function trim(str, pos = "both") {
  if (pos == "both") {
    return str.replace(/^\s+|\s+$/g, "");
  } else if (pos == "left") {
    return str.replace(/^\s*/, "");
  } else if (pos == "right") {
    return str.replace(/(\s*$)/g, "");
  } else if (pos == "all") {
    return str.replace(/\s+/g, "");
  } else {
    return str;
  }
}

/**
 * 根据数字获取对应的汉字
 * @param {number} num - 数字(0-10) */
export function getHanByNumber(num) {
  const str = "零一二三四五六七八九十";
  return str.charAt(num);
}

/**
 * 获取指定整数范围内的随机整数
 * @param {number} start - 开始范围
 * @param {number} end - 结束范围 */
export function getRandomInterger(start = 0, end) {
  const range = end - start;
  const random = Math.floor(Math.random() * range + start);
  return random;
}

/** @desc 千分位格式化 */
export function formatMoney(money) {
  return money.replace(
    new RegExp(`(?!^)(?=(\\d{3})+${money.includes(".") ? "\\." : "$"})`, "g"),
    ","
  );
}

/** @desc 数据类型检测方法 */
export function getTypeOf(value) {
  return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}

/**
 * @desc 格式化电话号码
 * @demo 183-7983-6654 */
export function formatPhone(mobile, formatStr = "-") {
  return mobile.replace(/(?=(\d{4})+$)/g, formatStr);
}

/**
 * @desc 手机号脱敏
 * @demo 155****8810  */
export function hidePhone(phone) {
  return phone.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
}

/**
 * @desc 身份证号脱敏
 * @demo 155****8810  */
export function hideId(phone) {
  return phone.replace(/^(.{6})(?:\d+)(.{4})$/, "$1****$2");
}

/** @desc 检测数据是否为空数据 */
export function isEmpty(data) {
  if (
    data === "" ||
    data === "undefined" ||
    data === undefined ||
    data === null ||
    data === "null"
  ) {
    return true;
  }
  if (
    JSON.stringify(data) == "{}" ||
    JSON.stringify(data) == "[]" ||
    JSON.stringify(data) == "[{}]"
  ) {
    return true;
  }
  return false;
}

/**
 * @desc 大小写转换
 * @param {string} str 待转换的字符串
 * @param {number} type 1:全大写 2:全小写 3:首字母大写 */
export function toCase(str, type) {
  switch (type) {
    case 1:
      return str.toUpperCase();
    case 2:
      return str.toLowerCase();
    case 3:
      return str[0].toUpperCase() + str.substring(1).toLowerCase();
    default:
      return str;
  }
}

/**
 * @desc 遍历树节点 */
export function foreachTree(data, callback, childrenName = "children") {
  for (let i = 0; i < data.length; i++) {
    callback(data[i]);
    if (data[i][childrenName] && data[i][childrenName].length > 0) {
      foreachTree(data[i][childrenName], callback, childrenName);
    }
  }
}

/**
 * @desc 获取随机数
 * @param {number} min 最小值
 * @param {number} max 最大值
 * */
export const randomNum = (min, max) => {
  return Math.floor(min + Math.random() * (max + 1 - min));
};

/**
 * @desc 获取最大值 */
export const max = (arr) => {
  return Math.max.apply(null, arr);
};

/**
 * @desc 获取最小值 */
export const min = (arr) => {
  return Math.min.apply(null, arr);
};

/**
 * @desc 求和 */
export const sum = (arr) => {
  return arr.reduce((pre, cur) => pre + cur);
};

/**
 * @desc 获取平均值 */
export const average = (arr) => {
  return sum(arr) / arr.length;
};

/**
 * @desc 文件大小格式化 */
export const formatFileSize = (size) => {
  const units = ["B", "KB", "MB", "GB"];
  let index = 0;
  while (size > 1024 && index < units.length) {
    size /= 1024;
    index++;
  }
  return Math.round(size * 100) / 100 + units[index];
};

/**
 * @desc 深拷贝 */
export const deepClone = (data) => {
  if (typeof data !== "object" || data === null) return "不是对象";
  const newData = Array.isArray(data) ? [] : {};
  for (const key in data) {
    newData[key] =
      typeof data[key] === "object" ? deepClone(data[key]) : data[key];
  }
  return newData;
};

/**
 * @desc 判断是否是闰年
 * @param {number} year 年份 */
export const isLeapYear = (year) => {
  return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
};

/**
 * @desc 判断是否是奇数
 * @param {number} num 数字 */
export const isOdd = (num) => {
  return num % 2 !== 0;
};

/**
 * @desc 判断是否是偶数
 * @param {number} num 数字 */
export const isEven = (num) => {
  return !isOdd(num);
};

/**
 * @desc 将RGB转化为十六机制 */
export const rgbToHex = (r, g, b) => {
  return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};

/**
 * @desc 获取随机十六进制颜色 */
export const randomHex = () => {
  return `#${Math.floor(Math.random() * 0xffffff)
    .toString(16)
    .padEnd(6, "0")}`;
};

export const deleteEmpty = (v) => {
  for (const key in v) {
    if (!v[key] && v[key] !== 0) {
      delete v[key];
    }
  }
  return v;
};

export const reAssign = (obj1, obj2) => {
  if (obj2) {
    for (const key in obj1) {
      obj1[key] = obj2[key] === 0 ? 0 : obj2[key] || undefined;
    }
  } else {
    for (const key in obj1) {
      obj1[key] = undefined;
    }
  }

  return obj1;
};

export const copyText = (text, tip = "复制成功", showTip = true) => {
  if (!text) return showToast("复制内容不存在");
  const timer = setTimeout(async () => {
    await toClipboard(text);
    if (showTip) {
      showToast(tip);
    }
    clearTimeout(timer);
  }, 0);
};

export const formatObject = (arr, comparison, value, label) => {
  const index = arr.findIndex((item) => {
    return item[comparison] == value;
  });
  return index > -1 ? arr[index][label] : "--";
};

export const getQueryString = (name, str) => {
  if (!str) {
    return null;
  }
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  let r = str.match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
};
